package com.wdh.kjs.activity;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.wdh.kjs.R;
import com.wdh.kjs.util.ToastUtils;

import org.apache.http.util.EncodingUtils;

/**
 * Created by codingWw on 2016/3/8.
 */
public class WebLoadActivity extends BaseTopActivity {

    public ProgressDialog mProgressDialog;
    public ProgressBar mProgressBar;
    public WebView mWebview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_load);

        initTopBar("详情");

        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setCancelable(true);

        mProgressBar = (ProgressBar) findViewById(R.id.pb_webview);
        mProgressBar.incrementProgressBy(1);
        mProgressBar.setVisibility(View.VISIBLE);
        mWebview = (WebView) findViewById(R.id.wv_service_item);
        mWebview.getSettings().setJavaScriptEnabled(true);//设置使用够执行JS脚本
        mWebview.getSettings().setBuiltInZoomControls(true);//设置使支持缩放
        String url = getIntent().getStringExtra("url");
        String params = getIntent().getStringExtra("params");
        if (TextUtils.isEmpty(params)) {
            mWebview.loadUrl(url);
        } else {
            mWebview.postUrl(url, EncodingUtils.getBytes(params, "utf-8"));
        }

        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                mProgressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mProgressBar.setVisibility(View.GONE);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                view.stopLoading();
                view.loadUrl("about:blank");
                view.loadUrl("");

                ToastUtils.showShort(WebLoadActivity.this, "加载失败,请检查网络。");
            }
        });
    }
}
